//
//  BlackListView.m
//  VoiceLive
//
//  Created by 凸阿滨 on 2017/10/24.
//  Copyright © 2017年 WB. All rights reserved.
//

#import "BlackListView.h"
#import "UIView+CustomAlertView.h"
#import "BlackListCell.h"
@interface BlackListView ()<UITableViewDelegate,UITableViewDataSource,BlackListCellDelegate>

@property (nonatomic,strong) UIControl *overlayView;
@end

NSString *listCell = @"listCellID";
@implementation BlackListView

+ (instancetype)creatBlackListView{
    
     return [[[NSBundle mainBundle]loadNibNamed:@"BlackListView" owner:nil options:nil]lastObject];
}

-(void)layoutSubviews{
    [super layoutSubviews];
  
    
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [self.layer setCornerRadius:6.0];
    [self.layer setMasksToBounds:YES];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 25;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BlackListCell class]) bundle:nil] forCellReuseIdentifier:listCell];

    [self creatData];
    
}

- (void)creatData{
    UFUserDefault *userInfo = [UFUserDefault shared];
    NSString *urlString = [[NetingAPI shared] urlStringWithModuleLiveTag:KUFBlackList];
    NSString *newString = [NSString stringWithFormat:@"%@/%@",urlString,userInfo.channel_id];
    NSDictionary *parameters = @{@"channel_id":userInfo.channel_id};
    [WBNetworking POST:newString parameters:parameters token:userInfo.redis_token success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.blackListArr = [BlackListData mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.blackListArr.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    BlackListCell *cell = [tableView dequeueReusableCellWithIdentifier:listCell];
    if(!cell){
        cell = [[BlackListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:listCell];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.list = self.blackListArr[indexPath.row];
    return cell;
    
    
}
#pragma mark BlackListCellDelegate
-(void)clickJiejin:(UIButton *)sender{
    UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
    // 获取cell的indexPath
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    BlackListData *list = self.blackListArr[indexPath.row];
   
    
    UFUserDefault *userInfo = [UFUserDefault shared];
    NSString *urlString = [[NetingAPI shared] urlStringWithModuleLiveTag:KUFRelve_Black];
    NSString *newString = [NSString stringWithFormat:@"%@/%@",urlString,userInfo.channel_id];
    NSDictionary *parameters = @{@"channel_id":userInfo.channel_id,@"send_id":list.send_id};
    [WBNetworking POST:newString parameters:parameters token:userInfo.redis_token success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"code"] isEqualToString:@"200"]){
//            [self creatData];
            [self.tableView beginUpdates];
            [self.blackListArr removeObject:list];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            [self.tableView endUpdates];
        }
       
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}




@end
